package com.example.k12.utlis;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.widget.TextView;

import com.example.k12.R;


/**
 * Created by hai.huang on 2018/5/22.
 */

public class DialogUtil {

    TextView msg;
    private Dialog progressDialog = null;

    private static DialogUtil instance = null;

    public static DialogUtil getInstance() {
        if (null == instance) {
            synchronized (DialogUtil.class) {
                if (null == instance) {
                    instance = new DialogUtil();
                }
            }
        }
        return instance;
    }

    /**
     * 取消所有弹出的对话框
     */
    public void dismissDialog() {
        try {
            if (progressDialog != null && progressDialog.isShowing())
                progressDialog.dismiss();
            progressDialog = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     *
     * @param context
     */
    public void showProgressDialog(Context context, String msgInfo) {
        try {
            if (context == null || ((Activity) context).isDestroyed()) {
                return;
            }
            dismissDialog();
            progressDialog = new Dialog(context, R.style.progress_dialog);
            progressDialog.setContentView(R.layout.dialog_progress);
            progressDialog.setCancelable(false);
            progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            msg = progressDialog.findViewById(R.id.tv_loadingmsg);
            msg.setText(msgInfo);
            progressDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * 更新进度
     * @param string
     */
    public void changeProgress(String string) {
        if (msg != null) {
            msg.setText(string);
        }
    }

    /***
     *
     * @param context
     */
    public void showLoginDialog(Context context) {
        dismissDialog();
        progressDialog = new Dialog(context, R.style.progress_dialog);
        progressDialog.setContentView(R.layout.dialog_progress);
        progressDialog.setCancelable(false);
        progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        TextView msg = progressDialog.findViewById(R.id.tv_loadingmsg);
        msg.setText("登录中...");
        progressDialog.show();
    }
}
